package com.fengchao.api.controller.v1;

import com.alibaba.fastjson.JSON;
import com.fengchao.api.service.MsgService;
import com.fengchao.api.service.UsrService;
import com.fengchao.model.entity.comm.CustomBizException;
import com.fengchao.model.entity.comm.Message;
import com.fengchao.model.entity.comm.ResponseParameter;
import com.fengchao.util.common.CommonValidator;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.fengchao.api.config.Constant.*;

/**
 * Created by wangnian on 2016/6/28.
 */
@RequestMapping("/v1/msg")
@RestController
public class MsgController  {

    @Autowired
    private MsgService msgService;
    @Autowired
    private UsrService usrService;

    private static Logger logger = LoggerFactory.getLogger(MsgController.class);

    @RequestMapping(value = "/submitSuggest", method = RequestMethod.POST)
    @ApiOperation(value = "(APP/WEB)提交反馈信息", notes = "返回参数</br>成功:</br>{status:SUCCESS, message:提交成功}" +
            "</br>失败:</br>{status:FAILURE, message:参数不全}" +
            "</br>{status:FAILURE, message:提交失败}")
    public ResponseParameter getUserMessage(@ApiParam(value = "(APP/WEB必填)类型,APP/WEB", required = true) @RequestHeader(required = false) String type,
                                            @ApiParam(value = "用户token") @RequestHeader(required = false) String token,
                                            @ApiParam(value = "(APP/WEB必填)文本内容", required = true) @RequestParam(required = true) String content,
                                            @ApiParam(value = "联系方式") @RequestParam(required = false) String moblie) {
        ResponseParameter responseParameter = new ResponseParameter(Message.FAILURE, SUBMIT_FAILURE);
        logger.info("提交反馈信息请求参数= type：{},token:{}, content:{} moblie:{}", type, token, content, moblie);
        if (!CommonValidator.isStringsNotNull(type)) {
            throw new CustomBizException(PARAMETER_IS_NOT_COMPLETE);
        }
        Integer userId = usrService.getUserId(type, token, false);
        Boolean bool = msgService.suggest(userId, content, moblie);
        if (bool) {
            responseParameter.setMessage(SUBMIT_SUCCESS);
            responseParameter.setStatus(Message.SUCCESS);
        }
        logger.info("提交反馈信息响应参数= {}", JSON.toJSON(responseParameter));
        return responseParameter;
    }


}
